package com.hanlink.dsp.protocols;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.util.BLog;
import com.hanlink.dsp.util.ByteUtil;
import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.sdk.model.entities.EqSetting;

/**
 * Created by zhanghb on 2015-11-28.
 */
public class DspProtocol {


    public static int[] getDefaultChannelValues() {
//        return new int[]{50, 125, 315, 750, 1250, 3200, 6000, 16000};
        return new int[]{
                  20,   25,   31,   40,     50,    63,    80,  100,
                 125,  160,  200,  250,    315,   400,   500,  630,
                 800, 1000, 1250, 1600,   2000,  2500,  3150, 4000,
                5000, 6300, 8000, 10000, 12500, 16000, 20000
        };
    }

    public static final int[][] EFFECT = new int[][]{{-1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 2, 2, 1, 0, 0, 0, 0, -1, -1, -1, -1},
            {8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0},
            {5, 6, 7, 5, 3, 1, 3, 4, 4, 6, 4, 2, 0, -1, -3, 0, 1, 2, 3, 4, 5, 6, 7, 5, 4, 6, 7, 6, 5, 4, 4},
            {6, 5, 5, 4, 3, 2, 1, 1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3},
            {-4, -4, -4, -4, -4, -4, -3, -2, -2, -2, -1, 2, 3, 4, 4, 6, 7, 9, 9, 7, 5, 4, 2, 1, 0, -1, -2, -2, -3, -3, -3},
            {7, 6, 5, 4, 5, 6, 7, 8, 9, 9, 8, 6, 4, 2, 0, 0, -1, -2, -3, -4, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2}
    };


    public static void resetEq() {
        byte[] EQ_CHANNEL = new byte[]{(byte) 0xAA, (byte) 0x55, (byte) 0xFF, (byte) 0x02, (byte) 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(EQ_CHANNEL);
    }

    public static void setDelay(int delay) {
        byte[] EQ_CHANNEL = new byte[]{(byte) 0xAA, (byte) 0x55, (byte) 0x52, (byte) 0x45, (byte) 0x4d, 0x53, 0x45, 0x54, (byte) ((delay>>8)&0xff), (byte) (delay&0xff), 0x00, 0x00, 0x00, 0x00};
        BLog.e("delay", ByteUtil.bytesToHexString(EQ_CHANNEL));
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(EQ_CHANNEL);
    }
    /**
     * 发送EQ协议数据
     */
    public static void  writeEqValue(int channel, int freqIndex, int q, int freqVal, int gain) {
        byte[] buffer = getEqBuffer(channel, freqIndex, q, freqVal, gain);
        BLog.d("eq",buffer.length +":"+ ByteUtil.bytesToHexString(buffer));
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);
    }

    public static byte[] getEqBuffer(int channel, int freqIndex, int q, int freqVal, int gain) {
        byte[] buffer = new byte[14];
//        {
//                (byte) (0xAA & 0xff),//1
//                (byte) (0x55 & 0xff),//1
//                (byte) (0xAA & 0xff),//1
//                0,//2.通道
//                0,//3.点
//                0,//4.频率高字节
//                0,//5.频率低字节
//                0,//6.q高字节
//                0,//7.q低字节
//                0,//8.增益高字节
//                0,//9.增益低字节
//                0,//10
//                0,//11
//                0,//12
//                0//13
//        };
        gain += SettingConstants.GAIN_OFFSET; // 120->200
        int i = 0;
        buffer[i++] = (byte) (0xAA & 0xff);
        buffer[i++] = (byte) (0x55 & 0xff);
        buffer[i++] = (byte) (0xAA & 0xff);
        buffer[i++] = (byte) ((0xC1 + channel) & 0xff);
        buffer[i++] = (byte) (freqIndex & 0xff);
        buffer[i++] = (byte) (freqVal >> 8 & 0xff);
        buffer[i++] = (byte) (freqVal & 0xff);
        buffer[i++] = (byte) (q >> 8 & 0xff);
        buffer[i++] = (byte) (q & 0xff);
        buffer[i++] = (byte) (gain >> 8 & 0xff);
        buffer[i++] = (byte) (gain & 0xff);
        return buffer;
    }

    /**
     * 联调协议发送
     */
    public static void writeDoubleEqValue(int groupIndex, int freqIndex, int q, int freqVal, int gain) {
        if (DspSettings.EQ_SETTINGS.isGroupAll()){

            byte[] buffer = getDoubleEqBuffer(0, freqIndex, q, freqVal, gain);
            BLog.d("eq", ByteUtil.bytesToHexString(buffer));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);

            buffer = getDoubleEqBuffer(1, freqIndex, q, freqVal, gain);
            BLog.d("eq", ByteUtil.bytesToHexString(buffer));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);

            buffer = getDoubleEqBuffer(2, freqIndex, q, freqVal, gain);
            BLog.d("eq", ByteUtil.bytesToHexString(buffer));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);

            buffer = getDoubleEqBuffer(3, freqIndex, q, freqVal, gain);
            BLog.d("eq", ByteUtil.bytesToHexString(buffer));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);
        }else{
            byte[] buffer = getDoubleEqBuffer(groupIndex, freqIndex, q, freqVal, gain);
            BLog.d("eq", ByteUtil.bytesToHexString(buffer));
            BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(buffer);
        }

    }

    public static byte[] getDoubleEqBuffer(int groupIndex, int freqIndex, int q, int freqVal, int gain) {
        byte[] buffer = new byte[14];
//        {
//                (byte) (0xAA & 0xff),//1
//                (byte) (0xAB & 0xff),//1
//                0,//2.通道
//                0,//3.点
//                0,//4.频率高字节
//                0,//5.频率低字节
//                0,//6.q高字节
//                0,//7.q低字节
//                0,//8.增益高字节
//                0,//9.增益低字节
//                0,//10
//                0,//11
//                0,//12
//                0//13
//        };
        byte group = (byte) 0xC1;
        switch (groupIndex) {
            case 0:
                group = (byte) 0xC1;
                break;
            case 1:
                group = (byte) 0xC3;
                break;
            case 2:
                group = (byte) 0xC5;
                break;
            case 3:
                group = (byte) 0xC7;
                break;
        }
        gain += SettingConstants.GAIN_OFFSET;
        int i = 0;
        buffer[i++] = (byte) (0xAA & 0xff);
        buffer[i++] = (byte) (0x55 & 0xff);
        buffer[i++] = (byte) (0xAB & 0xff);
        buffer[i++] = group;
        buffer[i++] = (byte) (freqIndex & 0xff);
        buffer[i++] = (byte) (freqVal >> 8 & 0xff);
        buffer[i++] = (byte) (freqVal & 0xff);
        buffer[i++] = (byte) (q >> 8 & 0xff);
        buffer[i++] = (byte) (q & 0xff);
        buffer[i++] = (byte) (gain >> 8 & 0xff);
        buffer[i++] = (byte) (gain & 0xff);
        return buffer;
    }


}
